package com.chijun.mform.process;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chijun.mform.create.mode.FormApproveProcess;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.create.mode.FormFieldOption;
import com.chijun.mform.create.mode.FormOptionReField;
import com.chijun.mform.full.mode.ModeFullForm;
import com.chijun.mform.full.mode.ModeFormFullApprovePerson;
import com.chijun.mform.full.mode.ModeFormFullFileData;
import com.example.ccj.u.JsonUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.StringUtils;
import com.example.ccj.u.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 获取到的表单填写的数据
 * 该表内数据在子线程中只读不写不需要考虑多线程安全
 */
public class ModeServerFullData {

    public String formName = "无名表单";
    public ModeFullForm launchedForm;                                       //表单配置

    public List<FormField> formFieldList;                                   //字段配置
    public Map<Long,List<ModeFormFullFileData>> formFileModeMap;                //文件 字段编号 字段文件列表
    public JSONObject formOrdinaryFieldDataJsonObject;                    //字段内容，这个包含选项 要将选项转换具体选项内容后返回  字段编号，字段值

    public List<FormApproveProcess> formApproveProcessList;                //流程
    public List<ModeFormFullApprovePerson> formApprovePeople;
    public Map<Long, List<ModeFormFullApprovePerson>> longFormApprovePersonMap;    //审批人   审批节点编号，审批人列表

    //废弃了，服务器未返回，因为选项字段不管是否有权限都不允许修改
    public Map<Long,List<FormFieldOption>> formFieldOptionMap;        //字段所有选项
    public Map<Long,List<Long>> formOptionReFieldMap;                   //表选项关联字段配置  Map<选项id,List<关联字段id>>

    public boolean isDoingPerson = false;                             //当前登录人员是否正在审批
    public ModeFormFullApprovePerson thisFormFullApprovePerson;                    //当前审批人
    public List<Long> canModifyFiledIdList;                         //当前登录人可修改的字段id列表

    public static ModeServerFullData analysis(String formFullData) {
        ModeServerFullData serverFullData = new ModeServerFullData();

        if (StringUtils.isEmpty(formFullData)) {
            LogUtils.e("解析数据不可为空");
            ToastUtils.showShort("解析数据不可为空！");
            return null;
        }
        JSONObject jsonObject = JSONObject.parseObject(formFullData);

        if (jsonObject.containsKey("form_name")) {
            String form_name = jsonObject.getString("form_name");
            if (!StringUtils.isEmpty(form_name)) {
                serverFullData.formName = form_name;
            }
        }

        //表单配置
        if (jsonObject.containsKey("full")) {
            String LaunchedFormStr = jsonObject.getString("full");
            if (!StringUtils.isEmpty(LaunchedFormStr)) {
                ModeFullForm launchedForm = JSON.parseObject(LaunchedFormStr, ModeFullForm.class);
                if (launchedForm != null) {
                    serverFullData.launchedForm = launchedForm;
                } else {
                    LogUtils.e("获取表单数据失败！解析完毕后为空！");
                    ToastUtils.showShort("获取表单数据失败！解析完毕后为空！");
                    return null;
                }
            } else {
                LogUtils.e("获取表单数据失败！返回的表单数据为空！");
                ToastUtils.showShort("获取表单数据失败！返回的表单数据为空！");
                return null;
            }
        } else {
            LogUtils.e("获取表单数据失败！返回的数据不包含表单配置数据！");
            ToastUtils.showShort("获取表单数据失败！返回的数据不包含表单配置数据！");
            return null;
        }

        serverFullData.formFieldList = new ArrayList<>();
        //字段
        if (jsonObject.containsKey("form_field")) {
            String fieldStr = jsonObject.getString("form_field");
            if (!StringUtils.isEmpty(fieldStr)) {
                List<FormField> formFields = JsonUtils.toListBean(fieldStr, FormField.class);

                if (formFields != null && !formFields.isEmpty()) {
                    serverFullData.formFieldList = formFields;
                    Collections.sort(serverFullData.formFieldList);
                } else {
                    LogUtils.e("未获取到该表单字段配置！");
                    ToastUtils.showShort("未获取到该表单字段配置！");
                    return null;
                }
            } else {
                LogUtils.e("获取到该表单字段配置为空！");
                ToastUtils.showShort("获取到该表单字段配置为空！");
                return null;
            }
        } else {
            LogUtils.e("服务器未返回字段配置数据！");
            ToastUtils.showShort("服务器未返回字段配置数据！");
            return null;
        }

        //文本数据
        if (jsonObject.containsKey("form_text_data")) {
            String formFileMode = jsonObject.getString("form_text_data");
//            LogUtils.d("返回的文本数据：" + formFileMode);
            if (!StringUtils.isEmpty(formFileMode)) {
                serverFullData.formOrdinaryFieldDataJsonObject = JSONObject.parseObject(formFileMode);
            }
        }

        serverFullData.formFileModeMap = new HashMap<>();
        //文件
        if (jsonObject.containsKey("form_file_mode")) {
            String formFileMode = jsonObject.getString("form_file_mode");
            if (!StringUtils.isEmpty(formFileMode)) {
                List<ModeFormFullFileData> formFileModeList = JsonUtils.toListBean(formFileMode, ModeFormFullFileData.class);
                if (formFileModeList != null && !formFileModeList.isEmpty()) {
                    for (ModeFormFullFileData fileMode : formFileModeList) {
                        if (serverFullData.formFileModeMap.containsKey(fileMode.reThree)) {
                            List<ModeFormFullFileData> fileModeList = serverFullData.formFileModeMap.get(fileMode.reThree);
                            if (fileModeList != null) fileModeList.add(fileMode);
                            else {
                                List<ModeFormFullFileData> fileModeList1 = new ArrayList<>();
                                fileModeList1.add(fileMode);
                                serverFullData.formFileModeMap.put(fileMode.reThree,fileModeList1);
                            }
                        } else {
                            List<ModeFormFullFileData> fileModeList = new ArrayList<>();
                            fileModeList.add(fileMode);
                            serverFullData.formFileModeMap.put(fileMode.reThree,fileModeList);
                        }
                    }
                }
            }
        }



        //选项
        serverFullData.formFieldOptionMap = new HashMap<>();
        if (jsonObject.containsKey("form_field_option")) {
            String formFieldOption = jsonObject.getString("form_field_option");
            if (!StringUtils.isEmpty(formFieldOption)) {
                List<FormFieldOption> formFieldOptionList = JsonUtils.toListBean(formFieldOption, FormFieldOption.class);
                if (formFieldOptionList != null && !formFieldOptionList.isEmpty()) {
                    serverFullData.formFieldOptionMap = new HashMap<>();
                    for (FormFieldOption fieldOption : formFieldOptionList) {
                        if (serverFullData.formFieldOptionMap.containsKey(fieldOption.fieldId)) {
                            List<FormFieldOption> formFieldOptions = serverFullData.formFieldOptionMap.get(fieldOption.fieldId);
                            if (formFieldOptions != null) formFieldOptions.add(fieldOption);
                        } else {
                            List<FormFieldOption> formFieldOptions = new ArrayList<>();
                            formFieldOptions.add(fieldOption);
                            serverFullData.formFieldOptionMap.put(fieldOption.fieldId,formFieldOptions);
                        }
                    }
                }
            }
        }

        //选项关联字段
        serverFullData.formOptionReFieldMap = new HashMap<>();
        if (jsonObject.containsKey("field_option_re")) {
            String formFieldOptionRe = jsonObject.getString("field_option_re");
            if (!StringUtils.isEmpty(formFieldOptionRe)) {
                List<FormOptionReField> formOptionReFieldList = JsonUtils.toListBean(formFieldOptionRe, FormOptionReField.class);
                if (formOptionReFieldList != null && !formOptionReFieldList.isEmpty()) {
                    serverFullData.formOptionReFieldMap = new HashMap<>();
                    for (FormOptionReField formOptionReField : formOptionReFieldList) {
                        if (serverFullData.formOptionReFieldMap.containsKey(formOptionReField.fieldOptionId)) {
                            List<Long> formOptionReFields = serverFullData.formOptionReFieldMap.get(formOptionReField.fieldOptionId);
                            if (formOptionReFields != null) formOptionReFields.add(formOptionReField.fieldId);
                        } else {
                            List<Long> formOptionReFields = new ArrayList<>();
                            formOptionReFields.add(formOptionReField.fieldId);
                            serverFullData.formOptionReFieldMap.put(formOptionReField.fieldOptionId,formOptionReFields);
                        }
                    }
                }
            }
        }

        //流程
        serverFullData.formApproveProcessList = new ArrayList<>();
        if (jsonObject.containsKey("form_approve_process")) {
            String formApproveProcess = jsonObject.getString("form_approve_process");
            if (!StringUtils.isEmpty(formApproveProcess)) {
                serverFullData.formApproveProcessList = JSON.parseArray(formApproveProcess, FormApproveProcess.class);
            }
        }

        //审批人
        serverFullData.longFormApprovePersonMap = new HashMap<>();
        if (jsonObject.containsKey("form_approve_person")) {
            String formApprovePerson = jsonObject.getString("form_approve_person");
//            LogUtils.d("审批人数据：" + formApprovePerson);
            if (!StringUtils.isEmpty(formApprovePerson)) {
                serverFullData.formApprovePeople = JSON.parseArray(formApprovePerson, ModeFormFullApprovePerson.class);
                for (ModeFormFullApprovePerson approvePerson : serverFullData.formApprovePeople) {
                    if (serverFullData.longFormApprovePersonMap.containsKey(approvePerson.processId)) {
                        serverFullData.longFormApprovePersonMap.get(approvePerson.processId).add(approvePerson);
                    } else {
                        List<ModeFormFullApprovePerson> f = new ArrayList<>();
                        f.add(approvePerson);
                        serverFullData.longFormApprovePersonMap.put(approvePerson.processId,f);
                    }
//                    LogUtils.d("审批人：" + approvePerson);
                }
            }
        }


        //是否当前审批人
        if (jsonObject.containsKey("isDoing")) {
            serverFullData.isDoingPerson = jsonObject.getBoolean("isDoing");
        }

        if (serverFullData.launchedForm.state == 1 && serverFullData.isDoingPerson) {
            //当前审批人
            if (jsonObject.containsKey("this_person")) {
                String thisPerson = jsonObject.getString("this_person");
                if (!StringUtils.isEmpty(thisPerson)) {
                    ModeFormFullApprovePerson launchedForm = JSON.parseObject(thisPerson, ModeFormFullApprovePerson.class);
                    if (launchedForm != null) {
                        serverFullData.thisFormFullApprovePerson = launchedForm;
                    } else {
                        LogUtils.e("获取当前审批人数据失败！解析完毕后为空！");
                        ToastUtils.showShort("获取当前审批人数据失败！解析完毕后为空！");
                        return null;
                    }
                } else {
                    LogUtils.e("获取当前审批人数据失败！返回的表单数据为空！");
                    ToastUtils.showShort("获取当前审批人数据失败！返回的表单数据为空！");
                    return null;
                }
            }
        }

        return serverFullData;
    }



}
